package com.dht.xiao.jianye.dlclogfile;


import net.sourceforge.pinyin4j.PinyinHelper;

import java.io.File;
import java.util.Comparator;

/**
 * 文字排序工具
 */
public class FileComparator implements Comparator<File> {
	/**
	 * true 逆序，false 顺序
	 */
	private boolean isNegative;

	/**
	 *
	 * @param isNegative
	 *            true 逆序，false 顺序
	 */
	public FileComparator(boolean isNegative) {
		super();
		this.isNegative = isNegative;
	}

	@Override
	public int compare(File lhs, File rhs) {
//		String gNameL = lhs.getName();
//		String gNameR = rhs.getName();

		if (isNegative) {
			return -compareWord(lhs.getName(), rhs.getName());
		} else {
			return compareWord(lhs.getName(), rhs.getName());
		}

	}

	private int compareWord(String ostr1, String ostr2) {

		for (int i = 0; i < ostr1.length() && i < ostr2.length(); i++) {

			int codePoint1 = ostr1.charAt(i);
			int codePoint2 = ostr2.charAt(i);
			if (Character.isSupplementaryCodePoint(codePoint1)
					|| Character.isSupplementaryCodePoint(codePoint2)) {
				i++;
			}
			if (codePoint1 != codePoint2) {
				if (Character.isSupplementaryCodePoint(codePoint1)
						|| Character.isSupplementaryCodePoint(codePoint2)) {
					return codePoint1 - codePoint2;
				}
				String pinyin1 = pinyin((char) codePoint1);
				String pinyin2 = pinyin((char) codePoint2);

				if (pinyin1 != null && pinyin2 != null) { // 两个字符都是汉字
					if (!pinyin1.equals(pinyin2)) {
						return pinyin1.compareTo(pinyin2);
					}
				} else {
					return codePoint1 - codePoint2;
				}
			}
		}
		return ostr1.length() - ostr2.length();
	}

	// 获得汉字拼音的首字符
	private String pinyin(char c) {
		String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
		if (pinyins == null) {
			return null;
		}
		return pinyins[0];
	}
}